<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['diy']=$this->plugin_setting('diy');
//日期 今天，未来七天
$data=array();
$data['0']=date("Y-m-d",time("day")); 
$data['1']=date("Y-m-d",strtotime("+1 day")); 
$data['2']=date("Y-m-d",strtotime("+2 day")); 
$data['3']=date("Y-m-d",strtotime("+3 day")); 
$data['4']=date("Y-m-d",strtotime("+4 day")); 
$data['5']=date("Y-m-d",strtotime("+5 day")); 
$data['6']=date("Y-m-d",strtotime("+6 day"));
if($_RQ['op']=='main'){	
	$serve=pdo_get('jy_qiuyuyue_timeserve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['serve_id']),array('title','times','price','serve_id'));
	$serve['servetitle']=pdo_getcolumn('jy_qiuyuyue_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$serve['serve_id']),'title');
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('nickname','mobile'));
	$return['post']=array(
		'goods_id'=>intval($_RQ['serve_id']),
		'serve_id'=>$serve['serve_id'],
		'store_id'=>intval($_RQ['store_id']),
		'price'=>$serve['price'],
		'username'=>$member['nickname'],
		'mobile'=>$member['mobile'],
		'lasttimes'=>$serve['times'],
	);
	$return['serve']=$serve;
	set_html('title','确认订单');
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if($postdata){
		if($postdata['fileds']){
			foreach ($postdata['fileds'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
				if($item['type']==7){
					$item['data']=$item['data']['province'].$item['data']['city'].$item['data']['district'].$item['data']['xiangxi'];	
				}
			}
		}
		$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'store_id'=>$postdata['store_id'],
				'serve_id'=>$postdata['serve_id'],
				'goods_id'=>$postdata['goods_id'],
				'remark'=>$postdata['remark'],
				'ordersn'=>get_ordersn('jy_qiuyuyue_timeserve_order','ordersn'),
				//'fileds'=>ijson_encode($postdata['fileds']),
				'price'=>$postdata['price'],
				'lasttimes'=>$postdata['lasttimes'],
				'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_qiuyuyue_timeserve_order',$insert);
		$id=pdo_insertid();
		$params=array(
				'id'=>$id,
				'title'=>'购买次卡',
				'fee'=>$postdata['price'],
				'plugin'=>'jy_qiuyuyue',
				//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
				'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
				'back_url'=>'jy_qiuyuyue/timesbook/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
				'success_url'=>'jy_qiuyuyue/timeserve_order/',
				'error_url'=>'jy_qiuyuyue/timeserve_order/',
		);
		$payid=cfc('pay')->create_pay_order($params);
		$this->pdo_update('timeserve_order',array('pay_id'=>$payid),array('id'=>$id));
		$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
		);
		exi($res);
	}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_qiuyuyue_timeserve_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_qiuyuyue_timeserve_order',array('status'=>'1'),array('id'=>$order['id']));
	if($this->plugin_exists('jy_commission')){
		//分销计入
		$order['plugin_name']='jy_qiuyuyue';
		model('jy_commission/commission')->create($order);
	}
	exi('支付成功','success','jy_qiuyuyue/timeserve_order/list/status=1&id='.$order['id']);
}